package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.Intent
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.argument.ItemArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.FragmentStateContainer
import com.gin.kotlin.no_gay_alliance.databinding.ActivityUseItemBinding
import com.gin.kotlin.no_gay_alliance.fragment.main.me.item.ItemInventoryFragment
import com.gin.kotlin.no_gay_alliance.fragment.main.me.item.ItemStoreFragment
import com.gin.nga.enums.param.ItemType
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class UseItemActivity : AbstractBindingActivity<ActivityUseItemBinding>(), TabLayoutMediator.TabConfigurationStrategy {
    private lateinit var tabLayoutMediator: TabLayoutMediator
    private lateinit var fragmentStateContainers: List<FragmentStateContainer>
    private lateinit var argument: ItemArgument

    override fun onCreate() {
        val b = this.b ?: throw RuntimeException("启动错误")
        //  FragmentState
        this.fragmentStateContainers = listOf(
            FragmentStateContainer(getString(R.string.fragment_tab_title_item_store)) { ItemStoreFragment.newInstance(argument) },
            FragmentStateContainer(getString(R.string.fragment_tab_title_item_inventory)) { ItemInventoryFragment.newInstance(argument) },
        )
        // 为ViewPager2设置适配器
        b.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = fragmentStateContainers.size
            override fun createFragment(position: Int) = FragmentStateContainer.newInstance(fragmentStateContainers, position)
        }
        this.tabLayoutMediator = attachTabLayout(b.tabLayout,  b.viewPager2, this)
    }

    override fun handleIntent(intent: Intent) {
        this.argument = parseArgument(intent, ItemArgument::class.java.simpleName, ItemArgument::class.java)
    }

    override fun onResume() {
        super.onResume()

        when (argument.type) {
            ItemType.TO_USER -> setToolbarTitle("对用户使用道具")
            ItemType.TO_REPLY -> setToolbarTitle("对主题/回复使用道具")
            else -> {}
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context, userId: Long) {
            val starter = Intent(context, UseItemActivity::class.java)
                .putExtra(ItemArgument::class.java.simpleName, ItemArgument(ItemArgument.Mode.Grid, type = ItemType.TO_USER, userId = userId))
            context.startActivity(starter)
        }

        @JvmStatic
        fun start(context: Context, topicId: Long, replyId: Long? = null) {
            val starter = Intent(context, UseItemActivity::class.java)
                .putExtra(ItemArgument::class.java.simpleName, ItemArgument(ItemArgument.Mode.Grid, type = ItemType.TO_REPLY, topicId = topicId, replyId = replyId))
            context.startActivity(starter)
        }

    }

    override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
        tab.text = fragmentStateContainers[position].title
    }
}